<?php

namespace app\model\user;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-UserLevelModel
 * Class UserLevelModel
 * @package app\model
 */
class UserLevelModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'user_level';

    /**
     * 获取分页列表
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @return array
     */
    public function getPageData($current_page, $list_rows, $field = '*')
    {
        $dataList = $this
            ->field($field)
            ->order('id', 'ASC')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * 获取列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function vipList()
    {
        $list = $this
            ->order("number asc")
            ->field("id,name,price")
            ->select();
        $rsps['code'] = 1;
        $rsps['data'] = $list;
        return $rsps;
    }

    /**
     * 获取会员等级id
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getLevelName()
    {
        $level = self::whereLike('name', '终生%')->field('id')->find();
        if (empty($level)) {
            return 0;
        }
        return $level['id'];
    }

    /**
     * 获取用户级别
     * @param int $lose 0正向 1负向
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getLevelReturn($lose = 0)
    {
        $list = self::field('name,number,self_return,direct_return,indirect_return,return_cash,lose_self_return,lose_direct_return,lose_indirect_return,lose_return_cash')->select();
        $return_arr = [];
        foreach ($list as $value) {
            if ($lose == 0) {
                $return_arr[$value['number']] = [
                    'name' => $value['name'],
                    'number' => $value['number'],
                    'self_return' => $value['self_return'],
                    'direct_return' => $value['direct_return'],
                    'indirect_return' => $value['indirect_return'],
                    'return_cash' => $value['return_cash'],
                ];
            }
            if ($lose == 1) {
                $return_arr[$value['number']] = [
                    'name' => $value['name'],
                    'number' => $value['number'],
                    'self_return' => $value['lose_self_return'],
                    'direct_return' => $value['lose_direct_return'],
                    'indirect_return' => $value['lose_indirect_return'],
                    'return_cash' => $value['lose_return_cash'],
                ];
            }
        }
        return $return_arr;
    }
}